﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class ExamPaperSelect : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        protected int _QuestionTypeid = 0, _SubjectId = 0, _VersionId = 0, _ExamId = 0, _KnowId = 0, _DiffId = 0;
        protected string _TitleStr = "";
        protected decimal _Score = 0;
        private string _PageUrl;
        protected void Page_Load(object sender, EventArgs e)
        {

            this.drpKnow.Attributes.Add("onchange ", "KnowChange();");
            this.tbKeyWord.Attributes.Add("onkeyup", "TxtKeyUp();");
            if (!UserValidate.CheckLogin(this.Request, "MasterUser"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            _PageUrl = "";
            if (Request.QueryString["page"] != null && Request.QueryString["page"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
                else
                {
                    Pages.PageNum = 1;
                }
            }
            else
            {
                Pages.PageNum = 1;
            }

            if (Request.QueryString["DiffId"] != null && Request.QueryString["DiffId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["DiffId"]))
                {
                    _DiffId = int.Parse(Request.QueryString["DiffId"]);
                    AddPageUrl("&DiffId=" + _DiffId.ToString());
                }
            }

            if (Request.QueryString["KnowId"] != null && Request.QueryString["KnowId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["KnowId"]))
                {
                    _KnowId = int.Parse(Request.QueryString["KnowId"]);
                    AddPageUrl("&KnowId=" + _KnowId.ToString());
                }
            }

            if (Request.QueryString["QuestionTypeId"] != null && Request.QueryString["QuestionTypeId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["QuestionTypeId"]))
                {
                    _QuestionTypeid = int.Parse(Request.QueryString["QuestionTypeId"]);
                    AddPageUrl("&QuestionTypeid=" + _QuestionTypeid.ToString());
                }
            }

            if (Request.QueryString["SubjectId"] != null && Request.QueryString["SubjectId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["SubjectId"]))
                {
                    _SubjectId = int.Parse(Request.QueryString["SubjectId"]);
                    AddPageUrl("&SubjectId=" + _SubjectId.ToString());
                }
            }
            if (Request.QueryString["VersionId"] != null && Request.QueryString["VersionId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["VersionId"]))
                {
                    _VersionId = int.Parse(Request.QueryString["VersionId"]);
                    AddPageUrl("&VersionId=" + _VersionId.ToString());
                }
            }

            if (Request.QueryString["Score"] != null && Request.QueryString["Score"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["Score"]))
                {
                    _Score = int.Parse(Request.QueryString["Score"]);
                    AddPageUrl("&Score=" + _Score.ToString());
                }
            }

            if (Request.QueryString["TitleStr"] != null && Request.QueryString["TitleStr"].Trim().Length > 0)
            {
                _TitleStr = Server.UrlDecode(Request.QueryString["TitleStr"]);
                AddPageUrl("&Score=" + Server.UrlEncode(_TitleStr));
            }

            if (Request.QueryString["ExamId"] != null && Request.QueryString["ExamId"].Trim().Length > 0)
            {
                if (PageValidate.IsNumber(Request.QueryString["ExamId"]))
                {
                    _ExamId = int.Parse(Request.QueryString["ExamId"]);
                    AddPageUrl("&ExamId=" + _ExamId.ToString());
                }
            }
            else
            {
                Response.Redirect("default.aspx");
            }
            if (!IsPostBack)
            {
                BindingQuestionType();
                BindingVersion();
                BindingDiff();
                BindingKnow();
                LoadList();
            }
            SetPostData();

        }

        void SetPostData()
        {
            if (!Page.IsPostBack)
            {
                if (_VersionId > 0)
                {
                    this.drpVerson.SelectedValue = _VersionId.ToString();
                }

                if (_DiffId > 0)
                {
                    this.drpDiff.SelectedValue = _DiffId.ToString();
                }

                if (_KnowId > 0)
                {
                    this.drpKnow.SelectedValue = _KnowId.ToString();
                }
                if (_QuestionTypeid > 0)
                {
                    drpQuestionType.SelectedValue = _QuestionTypeid.ToString();
                }
                if (_TitleStr != null && _TitleStr != "")
                {
                    this.tbTitle.Text = _TitleStr;
                }
                if (_Score > 0)
                {
                    this.tbScore.Text = _Score.ToString();
                }
            }
            else
            {
                if (this.drpVerson.SelectedValue != "")
                {
                    _VersionId = int.Parse(drpVerson.SelectedValue);
                }
                else
                {
                    _VersionId = 0;
                }
                if (this.drpDiff.SelectedValue != "")
                {
                    _DiffId = int.Parse(drpDiff.SelectedValue);
                }
                else
                {
                    _DiffId = 0;
                }
                if (this.drpKnow.SelectedValue != "")
                {
                    _KnowId = int.Parse(drpKnow.SelectedValue);
                }
                else
                {
                    _KnowId = 0;
                }
                if (this.drpQuestionType.SelectedValue != "")
                {
                    _QuestionTypeid = int.Parse(drpQuestionType.SelectedValue);
                }
                else
                {
                    _QuestionTypeid = 0;
                }
                if (this.tbTitle.Text != "")
                {
                    _TitleStr = this.tbTitle.Text;
                }
                else
                {
                    _TitleStr = null;
                }
                if (this.tbScore.Text != "")
                {
                    _Score = decimal.Parse(this.tbScore.Text);
                }
                else
                {
                    _Score = 0;
                }
            }
        }


        protected void LoadList()
        {
            Pages.AllCount = BAL.Question.GetQuestionsNotExamPaperIdCount(_ExamId, _TitleStr, _Score, _QuestionTypeid, _SubjectId, 0, _KnowId, "", _DiffId, _VersionId);
            Pages.PageSize = 20;
            Pages.PageName = "ExamPaperSelect.aspx?q=1" + _PageUrl + "&page=";
            Pages.Compute();
            PageLabel.Text = Pages.GetPage();
            IList<QuestionInfo> userinfos = BAL.Question.GetQuestionsNotExamPaperId(_ExamId, _TitleStr, _Score, _QuestionTypeid, _SubjectId, 0, _KnowId, "", _DiffId, _VersionId, Pages.First, Pages.Max);
            UserLists.DataSource = userinfos;
            UserLists.DataBind();
        }
        protected void lbTitle_DataBinding(object sender, EventArgs e)
        {

        }

        #region
        protected void lbDiff_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    else
                    {
                        DifficInfo item = Diffic.GetDifficInfoById(int.Parse(lb.Text));
                        if (item != null && item.Id != 0)
                        {
                            lb.Text = item.Name;
                        }
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        protected void lbKnow_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    else
                    {

                        KnowledgeInfo item = Knowledge.GetKnowledgeInfoById(int.Parse(lb.Text));
                        if (item != null && item.Id != 0)
                        {
                            lb.Text = item.Keyword + "&nbsp;&nbsp;" + item.Name;
                        }
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        protected void lbSubject_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    SubjectInfo item = Subject.GetSubjectInfoById(int.Parse(lb.Text));
                    if (item != null && item.Id != 0)
                    {
                        lb.Text = item.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        //protected void lbGrade_DataBinding(object sender, EventArgs e)
        //{
        //    Label lb = sender as Label;
        //    if (lb != null)
        //    {
        //        try
        //        {
        //            if (lb.Text == "" || lb.Text == "0")
        //            {
        //                lb.Text = "";
        //            }
        //            GradeInfo item = Grade.GetGradeInfoById(int.Parse(lb.Text));
        //            if (item != null && item.Id != 0)
        //            {
        //                lb.Text = item.Name;
        //            }
        //        }
        //        catch
        //        {
        //            lb.Text = "";
        //        }
        //    }
        //}
        protected void lbQuestionType_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    QuesTypeInfo item = QuesType.GetQuesTypeInfoById(int.Parse(lb.Text));
                    if (item != null && item.Id != 0)
                    {
                        lb.Text = item.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        #endregion

        private void Refresh()
        {

            this.ClientScript.RegisterStartupScript(this.GetType(), "key", "WinRefresh();", true);
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton lk = sender as LinkButton;
            if (lk != null)
            {
                try
                {
                    Paper_Ques.Add(new Paper_QueInfo() { QuesId = int.Parse(lk.CommandArgument), ExamPaperId = _ExamId, CreateDate = DateTime.Now });
                    SetUrlParams();
                    LoadList();
                    Refresh();
                }
                catch
                {

                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetUrlParams();
            LoadList();
        }

        private void SetUrlParams()
        {
            _PageUrl = "";
            AddPageUrl("&ExamId=" + _ExamId.ToString());
            AddPageUrl("&SubjectId=" + _SubjectId.ToString());
            if (tbTitle.Text.Trim().Length > 0)
            {
                _TitleStr = tbTitle.Text.Trim();
                AddPageUrl("&Title=" + Server.UrlEncode(_TitleStr));
            }
            if (tbScore.Text.Trim().Length > 0)
            {
                if (PageValidate.IsNumber(tbScore.Text.Trim()))
                {
                    _Score = int.Parse(tbScore.Text.Trim());
                    AddPageUrl("&Score=" + _Score.ToString());
                }
            }
            if (drpQuestionType.SelectedValue.Trim().Length > 0)
            {
                if (PageValidate.IsNumber(drpQuestionType.SelectedValue.Trim()))
                {
                    _QuestionTypeid = int.Parse(drpQuestionType.SelectedValue.Trim());
                    AddPageUrl("&QuestionTypeid=" + _QuestionTypeid.ToString());
                }
            }




            if (drpVerson.SelectedValue.Trim().Length > 0)
            {
                if (PageValidate.IsNumber(drpVerson.SelectedValue.Trim()))
                {
                    _VersionId = int.Parse(drpVerson.SelectedValue.Trim());
                    AddPageUrl("&VersionId=" + _VersionId.ToString());
                }
            }

            if (this.drpDiff.SelectedValue.Trim().Length > 0)
            {
                if (PageValidate.IsNumber(drpDiff.SelectedValue.Trim()))
                {
                    _DiffId = int.Parse(drpDiff.SelectedValue.Trim());
                    AddPageUrl("&DiffId=" + _DiffId.ToString());
                }
            }
            if (this.drpKnow.SelectedValue.Trim().Length > 0)
            {
                if (PageValidate.IsNumber(drpKnow.SelectedValue.Trim()))
                {
                    _KnowId = int.Parse(drpKnow.SelectedValue.Trim());
                    AddPageUrl("&KnowId=" + _KnowId.ToString());
                }
            }



        }

        void BindingQuestionType()
        {
            this.drpQuestionType.Items.Clear();
            this.drpQuestionType.Items.Add(new ListItem("选择", ""));
            IList<QuesTypeInfo> list = QuesType.GetQuesTypeInfos(0, int.MaxValue);
            foreach (QuesTypeInfo info in list)
            {
                drpQuestionType.Items.Add(new ListItem(info.Name, info.Id.ToString()));
            }

        }

        void BindingSub()
        {

        }
        void BindingVersion()
        {
            drpVerson.Items.Clear();
            if (this._SubjectId > 0)
            {
                IList<BookInfo> list = Book.GetBookInfosBySubject(_SubjectId);
                this.drpVerson.DataTextField = "Name";
                this.drpVerson.DataValueField = "Id";
                this.drpVerson.DataSource = list;
                this.drpVerson.DataBind();

            }
            drpVerson.Items.Insert(0, new ListItem("选择", ""));


        }

        void BindingDiff()
        {
            drpDiff.Items.Clear();
            IList<DifficInfo> list = Diffic.GetDifficInfos(0, int.MaxValue);
            this.drpDiff.DataTextField = "Name";
            this.drpDiff.DataValueField = "Id";
            this.drpDiff.DataSource = list;
            this.drpDiff.DataBind();
            drpDiff.Items.Insert(0, new ListItem("选择", ""));


        }

        void BindingKnow()
        {
            drpKnow.Items.Clear();
            IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetRootKnowledgeInfo(0, _SubjectId);
            foreach (KnowledgeInfo info in knowledgeInfos)
            {
                ListItem item = new ListItem(info.Keyword + "  " + info.Name, info.Id.ToString());//+ "    " + info.Keyword
                drpKnow.Items.Add(item);
                //item = new ListItem(info.Keyword, info.Keyword);
                LoadKnowledge(info.Id, _SubjectId, "|——");
            }
            drpKnow.Items.Insert(0, new ListItem("选择", ""));


        }

        protected void LoadKnowledge(int ParentID, int inputSubjectId, string line)
        {
            IList<KnowledgeInfo> knowledgeInfos = Knowledge.GetKnowledgeInfos(ParentID);//通过parentId获取子集合
            foreach (KnowledgeInfo info in knowledgeInfos)
            {
                ListItem item = new ListItem(line + info.Keyword + "  " + info.Name, info.Id.ToString());//+ "    " + info.Keyword
                drpKnow.Items.Add(item);
                LoadKnowledge(info.Id, inputSubjectId, "|——" + line);
            }
        }

        protected void drpSub_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        protected void drpVerson_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem item in this.UserLists.Items)
                {
                    HtmlInputCheckBox chk = item.FindControl("ckItem") as HtmlInputCheckBox;
                    if (chk != null && chk.Checked)
                    {
                        Paper_Ques.Add(new Paper_QueInfo() { QuesId = int.Parse(chk.Value), ExamPaperId = this._ExamId, CreateDate = DateTime.Now });
                    }
                }
                SetUrlParams();
                LoadList();
                Refresh();
            }
            catch
            {

            }
        }

        private void AddPageUrl(string InsStr)
        {
            _PageUrl += InsStr;
        }
    }
}

